const Service = require("uni-cloud-router").Service;
module.exports = class UserService extends Service{
	async getUnderwayProject({wkID,projectStatus}){
		const dbCmd = this.db.command;
		let res = await this.db.collection('project_store').where({
			project_status:projectStatus,
			worker_ids:dbCmd.elemMatch({
				wkID:wkID
			})
		}).get()
		
		if(res.affectedDocs !== 0 || res.data.length !== 0){
			return{
				status:0,
				msg:"成功",
				data:res.data
			}
		} else {
			return {
				status:1,
				msg:"没有数据"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async getWorkerInfo({projectID,wkID,pageNo,pageSize}){
		let res = await this.db.collection('signin_store')
		.aggregate()
		.match({
			projectID,
			wkID
		})
		.skip((pageNo-1)*pageSize)
		.limit(pageSize)
		.end()
		
		if(res.affectedDocs !== 0 || res.data.length !== 0){
			return {
				status:0,
				msg:"成功",
				data:res.data
			}
		} else {
			return {
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async getWKhistoryProject({id,type}){
		/**
		 * "onProjectID":根据项目ID获取
		 * "onWKid":根据员工ID获取
		 */
		let event = this.ctx.data;
		let querys = {}
		type == 'onWKid' ? querys = {wkID:event.wkID} : querys = {projectID:id};
		let res = await this.db.collection('wk_history_project').where(querys).get();
		
		if(res.affectedDocs !== 0 || res.data.length !== 0){
			return{
				status:0,
				msg:"成功",
				data:res.data
			}
		} else {
			return {
				status:1,
				msg:"稍后重试"
			}
		}
		
		//返回数据给客户端
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
}